<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Project"%>
<%@page import="db.TobDAO" %>
<%@page import="db.Tob" %>
<%@page import="db.TeamDAO" %>
<%@page import="db.Team" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="db.Supplier" %>
<%@page import="db.SupplierDAO" %>
<%@page import="db.User"%>
<%@page import="db.UserDAO"%>
<%@page import="db.Group"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	PurreqDAO purreqDAO = (PurreqDAO)ctx.getBean("PurreqDAO");
	TobDAO tobDAO = (TobDAO)ctx.getBean("TobDAO");
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	TeamDAO teamDAO = (TeamDAO)ctx.getBean("TeamDAO");
	SupplierDAO supplierDAO = (SupplierDAO)ctx.getBean("SupplierDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	PurmatDAO purmatDAO = (PurmatDAO)ctx.getBean("PurmatDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	String pid = request.getParameter("pid");
	Purreq purreq = new Purreq();
	Tob tob = new Tob();
	Team team = new Team();
	Project project = new Project();
	Supplier supplier = new Supplier();
	User requser = new User();
	User buyuser = new User();
	Material material = new Material();
	List purmatlist = new ArrayList();
	if(pid!=null){
		purreq = purreqDAO.findById(Integer.parseInt(pid));
		purmatlist = purmatDAO.findByPurreqId(Integer.parseInt(pid));
	}
	String tobName = "";
	if(purreq!=null){//查询业务类型
		int tid = 0;
		tid = purreq.getTobId();
		project =  purreq.getProject();
		tob = tobDAO.findById(tid);
		tobName = tob.getName();
		int teamid = 0;
		teamid = purreq.getTeamId();
		int suppid = 0;
		suppid = purreq.getSupplierId();
		team = teamDAO.findById(teamid);
		supplier = supplierDAO.findById(suppid);
		int requid = 0;
		
		if(purreq.getReqUserId()!=null){
			requid = purreq.getReqUserId();
			requser = userDAO.findById(requid);
		}
		int buyuid = 0;
		if(purreq.getBuyUserId()!=null){
			buyuid = purreq.getBuyUserId();
			buyuser = userDAO.findById(buyuid);
		}
		
		
	}
	Group reqGroup = new Group();
	Group buyGroup = new Group();
	if(requser!=null){
		reqGroup = requser.getGroup();
	}
	if(buyuser!=null){
		buyGroup = buyuser.getGroup();
	}
	boolean senpi = false;
	int userid = (Integer)session.getAttribute("id");
	if(purreq.getBuyUserId()!=null && purreq.getBuyUserId()==userid){
		senpi=true;
	}else if(purreq.getBuyUserId()==null){
		senpi=true;
	}
	request.setAttribute("purmatlist",getListReMap2(purmatlist,materialDAO));
	request.setAttribute("reqGroup",reqGroup);
	request.setAttribute("requser",requser);
	request.setAttribute("buyGroup",buyGroup);
	request.setAttribute("buyuser",buyuser);
	request.setAttribute("supplier",supplier);
	request.setAttribute("team",team);
	request.setAttribute("project",project);
	request.setAttribute("tobName",tobName);
	request.setAttribute("purreq",purreq);
	request.setAttribute("senpi",senpi);
	request.setAttribute("pid",pid);
	
	
	
	
	pageContext.forward("/vm/procure/procureApp_1.vm");
%>
<%!
	public List getListReMap(List list){
		List listmap = new ArrayList();
		//System.out.println("===============>>>>>>>>");
		if(list != null && list.size()>0){
		
			for(int i=0;i<list.size();i++){
			
				Map map = new HashMap();
				Object[] aa= (Object[]) list.get(i);
				map.put("pid", aa[0]);
				map.put("pstate", aa[1]);
				map.put("pcode", aa[2]);
				map.put("procode", aa[3]);
				map.put("proname", aa[4]);
				SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); 
				if(aa[5]!=null){
					map.put("pdate", sf.format(aa[5]));
				}
				
				if(aa[6]!=null){
					map.put("penddate", sf.format(aa[6]));
				}
				
				listmap.add(map);
			}
		}
		return listmap;
	}
	
	public List getListReMap2(List list,MaterialDAO materialDAO){
		List listmap = new ArrayList();
		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = new HashMap();
				Purmat purmat = new Purmat();
				purmat = (Purmat)list.get(i);
				if(purmat!=null && purmat.getState()==1){
					map.put("purid", purmat.getId());//
					map.put("purnum", purmat.getNumber());//申购数量
					map.put("purprice",purmat.getPrice());//申购价格
					//根据物质id关联物质编码和名称
					if(purmat.getMaterialId()!=null){
						int mid = purmat.getMaterialId();
						Material material = new Material();
						material = materialDAO.findById(mid);
						map.put("matcode", material.getCode());
						map.put("matname", material.getName());
						map.put("matcom", material.getCompany());
					}
					listmap.add(map);
				}
				
			}
		}
		return listmap;
	}
%>